// pages/withdraw/withdraw.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canClick: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
   
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,

        })
      }
    })
  },
  onShow:function(){
    getBalance(this);
  },
  getDrawNumber:function(e){
    if (e.detail.value != '') {
      this.setData({
        getInputMoney: e.detail.value
      })
    }else{
      this.setData({
        getInputMoney: 0
      })
    }
  }
  ,
  withDrawMoney:function(){
    var that = this;
    if (this.data.canClick) {
      that.setData({
        canClick: false
      })
      setTimeout(function () {
        that.setData({
          canClick: true
        })
      }, 3000)
    } else {
      wx.showToast({
        title: '处理中，请勿重复提交',
      })
      return;
    }
    var reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
    if (!reg.test(this.data.getInputMoney)) {
      wx.showModal({
        title: '提示',
        content: '红包金额必须为数字或者小数',
      })
      return;
    }
   
    if (parseFloat(this.data.getInputMoney) > parseFloat(this.data.banlace) || parseFloat(this.data.getInputMoney) > parseFloat(1000) || parseFloat(this.data.getInputMoney) < parseFloat(1)) {
      wx.showModal({
        title: '提示',
        content: '余额不足！',
      })
      return;
    }
    if (parseFloat(this.data.getInputMoney) < parseFloat(1)) {
      wx.showModal({
        title: '提示',
        content: '单次最少提现金额不得小于1元',
      })
      return;
    }
    if (parseFloat(this.data.getInputMoney) > parseFloat(1000)) {
      wx.showModal({
        title: '提示',
        content: '单次最高提现金额不得超过1000元',
      })
      return;
    }
    console.log(this.data.getInputMoney)
    witsDrawMoney(this, this.data.getInputMoney)
  }
})

function getBalance(that) {
  wx.request({
    url: app.globalData.allUrl + "/api/my_money.php",
    data: {
      user_id: wx.getStorageSync('userid'),

    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      //   console.log(res.data)
      that.setData({
        banlace: res.data.data
      })
    }, fail: function () {
    }
  })
}

function witsDrawMoney(that,_money){
  wx.request({
    url: app.globalData.allUrl + "/api/withdrawals.php",
    data: {
      user_id: wx.getStorageSync('userid'),
      money: _money,
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
         console.log(res.data)
      if (res.data.state==200){
        wx.request({
          url: app.globalData.allUrl + "/api/enter_payment.php",
          data: {
            user_id: wx.getStorageSync('userid'),
            money: _money,
            order_id: res.data.order_id
          },
          header: { 'content-type': "application/x-www-form-urlencoded" },
          method: 'POST',
          success: function (res) {
            console.log(res.data)
            if (res.data.state==200){
              wx.showModal({
                title: '提示',
                content: '提现成功',
              })
            } else if (res.data.state == 400) {
              wx.showModal({
                title: '提示',
                content: '服务器维护中，请稍后重试',
              })
            }
            
            getBalance(that)
          }, fail: function () {
          }
        })   
      }
    }, fail: function () {
    }
  })
  
}